<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Department extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('muser');
   $this->load->library('form_validation');
   $this->load->library('pagination');
 }
 function index(){ 	
 	 if ($this->input->post('Department_ID')!=NULL && $this->input->post('Department')!=NULL)
 	 {
 	 	$this->form_validation->set_rules('Department_ID','Department_ID','trim|callback_check_Department');
		$this->form_validation->set_rules('Department','Department','trim');  		
		if ($this->form_validation->run()==FALSE){
			$data['error']='Department_ID is existed!';
		}else{
			$DT['Department_ID']=$this->input->post('Department_ID');	
		 	$DT['Department']=$this->input->post('Department');
			$this->muser->addData('Department',$DT);
			$data['error']='insert successful!';
		} 				
 	 }	
 	 $config['base_url'] = base_url().'department/index'; 
	 $config['total_rows'] = $this->muser->num_rows('Department');
	 
	 $config['per_page'] = 10; // xác định số record ở mỗi trang  
     $config['uri_segment'] = 3; // xác định segment chứa page number  
	 $this->pagination->initialize($config); 
	 $data['result']=$this->muser->getalldata('Department',$config['per_page'],$this->uri->segment(3));	
	 
	 if ($error=$this->session->userdata('error')) {
	 	$data['error']=$error;
		$this->session->unset_userdata('error'); 
	 }
	 $this->load->view('_header');
     $this->load->view('department/department_view',$data); 
     $this->load->view('_footer');
 }
 function delete($Department_ID){
 	if ($this->muser->checkData('Session','Department_ID',$Department_ID))
	{
		$this->muser->delColumn('Department','Department_ID',$Department_ID);
		redirect('department/index');	
	} else {
		$error['error']= $Department_ID.' is a foreign key!';
		$this->session->set_userdata($error);
		redirect('department/index');
	}		
 }
 
function edit1 ()
 {
 	$DT['Department_ID']=$this->input->post('Department_ID');	
	$DT['Department']=$this->input->post('Department');
	$this->muser->updateData('Department','Department_ID',$DT['Department_ID'],$DT);
	$data['error']='Edit successful!';	 	
	redirect('department/index');		
 } 
 function edit($str)
 {	
 	$result=$this->muser->getRdata('Department','Department_ID',$str);
	$data['result']=$result;	
 	$this->load->view('_header');
	$this->load->view('department/editDepartment_view',$data);	
	$this->load->view('_footer');
 }
 
 function check_Department($str){
 		return $this->muser->checkData('Department','Department_ID',$str);		
 }
}